package com.example.springcloud.common;

import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/8/4 11:36
 * @since JDK 1.8
 */
public class HttpUtils {

    public static String getPost(String url, Map<String, String> headsParam,Map<String, Object> paramsObject){
        OkHttpUtils okHttpUtils=OkHttpUtils.builder();
        okHttpUtils.url(url);
        okHttpUtils=addHeadParams(okHttpUtils,headsParam);
        return okHttpUtils.post(paramsObject).async();
    }

    private static  OkHttpUtils addHeadParams(OkHttpUtils okHttpUtils,Map<String, String> headParams){
        if(headParams!=null){
            for (Map.Entry<String, String> stringEntry:headParams.entrySet()){
                String headName=stringEntry.getKey();
                String haedValue=stringEntry.getValue();
                okHttpUtils.addHeader(headName,haedValue);
            }
        }
        return okHttpUtils;
    }

    public static String getPost(String url,Map<String, Object> paramsObject){
        return getPost(url,null,paramsObject);
    }

    public static String getGet(String url,Map<String, String> headParams,Map<String, String> paramObject){
        OkHttpUtils okHttpUtils=OkHttpUtils.builder();
        okHttpUtils.url(url);
        okHttpUtils=addHeadParams(okHttpUtils,headParams);
        if(paramObject!=null){
            for (Map.Entry<String, String> stringEntry:headParams.entrySet()){
                String paramName=stringEntry.getKey();
                String paramValue=stringEntry.getValue();
                okHttpUtils.addParam(paramName,paramValue);
            }
        }
        return okHttpUtils.get().async();
    }
}
